﻿
using System;
using SqlSugar;
using System.Threading.Tasks;
using System.Linq.Expressions;

using Oliving.Edge.Model;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IRepository;
using Oliving.Edge.IRepository.UnitOfWork;
using Oliving.Edge.Repository.Base;


using Oliving.Edge.Model.School;
using Oliving.Edge.Model.Dto.School;
using Oliving.Edge.IRepository.School;

namespace Oliving.Edge.Repository.School
{
    /// <summary>
    /// School_AchievementRepository
    /// </summary>
    public class School_AchievementRepository : BaseRepository<School_Achievement>, ISchool_AchievementRepository
    {
        public School_AchievementRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }


        /// <summary>
        /// 自定义分页查询
        /// </summary>
        /// <param name="whereExpression">查询条件</param>
        /// <param name="pageIndex"> 页码（下标0）</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="sortOrder">排序顺序（asc/desc）</param>
        /// <returns></returns>
        public async Task<PageModel<School_AchievementPageDto>> QueryPageList(
            Expression<Func<School_Achievement, bool>> whereExpression,
            int pageIndex = 1, int pageSize = 20,
            string sortField = null, string sortOrder = null
            )
        {


            #region 排序

            string strOrderByFileds = null;

            if (!string.IsNullOrEmpty(sortField))
            {
                strOrderByFileds = sortField;
                if (sortOrder != null && sortOrder.ToLower().Contains("desc"))
                {
                    strOrderByFileds = strOrderByFileds + " desc";
                }
                else
                {
                    strOrderByFileds = strOrderByFileds + " asc";
                }
            }

            #endregion

            if (1 == 1)
            {
                #region 单表

                RefAsync<int> totalCount = 0;
                var list = await Db.Queryable<School_Achievement>()
                         .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                         .WhereIF(whereExpression != null, whereExpression)
                         .Select<School_AchievementPageDto>()
                         .ToPageListAsync(pageIndex, pageSize, totalCount);

                int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt();
                return new PageModel<School_AchievementPageDto>()
                {
                    dataCount = totalCount,
                    pageCount = pageCount,
                    page = pageIndex,
                    PageSize = pageSize,
                    data = list
                };

                #endregion
            }

            {
                //var list = Db.Queryable<DTSN_Canning, DTSN_TongDao>
                //   ((c, td) => new object[] { JoinType.Left, c.Canning_TongDaoID == td.TD_ID })
                // //.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                // .WhereIF(whereExpression != null, whereExpression)
                // .GroupBy((c, td) => td.TD_Type)
                // .GroupBy((c, td) => Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString())
                //   .Select((c, td) => new
                //   {
                //       date = Convert.ToDateTime(c.Canning_BeginTime).ToShortDateString(),
                //       type = td.TD_Type,
                //       sum = SqlFunc.AggregateSum(c.Canning_NetWeight)
                //   }).ToList();
            }


        }
   
    
    
    }
}

